message(NOTICE "开始构建 yaml_cpp 模块")
# 检查构建目标是否存在
if(TARGET yaml-cpp-${YAML_CPP_TAG})
    message(NOTICE "yaml_cpp 模块已存在 yaml-cpp-${YAML_CPP_TAG}")
    return()
endif()
# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/yaml-cpp/${YAML_CPP_TAG}
    ${MODULE_INSTALL_DIR}/yaml-cpp/${YAML_CPP_TAG}
    yaml-cpp-${YAML_CPP_TAG})
# 配置库参数成成规则
set(YAML_CPP_CONFIGURE CACHE STRING "yaml_cpp configure" FORCE)
if(BUILD_SHARED_LIBS)
    # 开启 编译动态库 ON
    list(APPEND YAML_CPP_CONFIGURE "-DBUILD_SHARED_LIBS=ON")
    list(APPEND YAML_CPP_CONFIGURE "-DYAML_BUILD_SHARED_LIBS=ON")
else()
    # 关闭 编译动态库 OFF
    list(APPEND YAML_CPP_CONFIGURE "-DBUILD_SHARED_LIBS=OFF")
    list(APPEND YAML_CPP_CONFIGURE "-DYAML_BUILD_SHARED_LIBS=OFF")
endif()
# 构建 yaml_cpp 库
ExternalProject_Add(
    yaml-cpp-${YAML_CPP_TAG}
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/yaml-cpp/${YAML_CPP_TAG}          # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/yaml-cpp/${YAML_CPP_TAG}        # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/yaml-cpp/${YAML_CPP_TAG}  # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/yaml-cpp/${YAML_CPP_TAG}        # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/yaml-cpp/${YAML_CPP_TAG}          # ✅ 显式设置 tmp 目录

    DOWNLOAD_COMMAND # 下载命令
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${YAML_CPP_REPO}
            -DGIT_TAG=${YAML_CPP_TAG}
            -P ${__DIR}/utils/git.cmake
    CONFIGURE_COMMAND
        COMMAND ${CMAKE_COMMAND}
             -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
             -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
             # 标准 C/C++ 编译选项
             -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
             -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
             -DBUILD_TESTING=OFF # 关闭测试
             -DYAML_CPP_BUILD_CONTRIB=OFF # 关闭贡献模块
             -DYAML_CPP_BUILD_TESTS=OFF # 关闭测试模块
             -DYAML_CPP_BUILD_TOOLS=OFF # 关闭工具模块
             -DYAML_CPP_FORMAT_SOURCE=OFF # 开启格式化源文件
             -DYAML_CPP_INSTALL=ON # 开启安装
             -DCMAKE_POSITION_INDEPENDENT_CODE=ON
            -S <SOURCE_DIR>                       # ✅ 显式设置 source 目录
            -B <BINARY_DIR>                       # ✅ 显式设置 build 目录
            ${YAML_CPP_CONFIGURE}

    BUILD_COMMAND
        ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE} -j${NPROC}
    INSTALL_COMMAND
        ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
            # 将安装文件路径保存到 文件去
            COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     OFF                           # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志
   # 依赖目标 安装需要先安装 automake libtool autopoint
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)

# 创建导入目标
add_library(YAML_CPP::YAML_CPP STATIC IMPORTED)
# 导入目标属性设置
set_target_properties(YAML_CPP::YAML_CPP PROPERTIES
    VERSION ${YAML_CPP_TAG}
    IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
    IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/yaml-cpp/${YAML_CPP_TAG}/lib/libyaml-cppd.a
    INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/yaml-cpp/${YAML_CPP_TAG}/include
)
#依赖库
add_dependencies(YAML_CPP::YAML_CPP yaml-cpp-${YAML_CPP_TAG})
